package fonkin.com.wifisignapp.adapter.news;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import java.util.Collection;
import java.util.List;

import fonkin.com.wifisignapp.R;
import fonkin.com.wifisignapp.bean.news.NewsInfor;
import fonkin.com.wifisignapp.bean.schedule.Schedule;
import fonkin.com.wifisignapp.constants.AppConstants;
import fonkin.com.wifisignapp.retrofit.BaseRetrofit;
import fonkin.com.wifisignapp.utils.ReturnVo;
import fonkin.com.wifisignapp.utils.ShowToast;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

import static com.squareup.picasso.MemoryPolicy.NO_CACHE;
import static com.squareup.picasso.MemoryPolicy.NO_STORE;

/**
 * @Description:新闻列表Adapter
 * @Author: huanghongjing
 * @Date: 2018/4/7 23:43
 * No such property: code for class: Script1
 *
 */

public class ScheduleAdapter extends BaseAdapter {
    private Context context;
    private List<Schedule> scheduleInfoList;

    public ScheduleAdapter(Context context, List<Schedule> scheduleInfoList) {
        this.context = context;
        this.scheduleInfoList = scheduleInfoList;
    }

    @Override
    public int getCount() {
        return scheduleInfoList.size();
    }

    @Override
    public Object getItem(int position) {
        return scheduleInfoList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }


    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        if (convertView==null){
            convertView = LayoutInflater.from(context).inflate(R.layout.procedure_item,parent,false);
            convertView.setTag(new Viewholder(convertView));
        }
        Viewholder tag = (Viewholder) convertView.getTag();
        Schedule schedule = scheduleInfoList.get(position);
        tag.procedureType.setText(schedule.getScheduleType());
        tag.proBeginTime.setText(schedule.getBeginDateStr());
        tag.proEndTime.setText(schedule.getEndDateStr());
        tag.numDay.setText("天数"+schedule.getDayNum().toString());
        if (schedule.getIsApprove().equals(1)){
            tag.isFinish.setText("经办人："+schedule.getUpdator().toString());
        }else {
            tag.isFinish.setText(schedule.getIsApproveStr().toString());
        }
        tag.delBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Dialog dialog = new AlertDialog.Builder(context)
                        .setTitle("提示！")
                        .setMessage("是否要移除这条记录")
                        .setIcon(R.drawable.ic_error_outline_black_24dp)
                        .setPositiveButton("移除", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                delData(position);
                            }
                        })
                        .setNegativeButton("取消", null)
                        .create();
                dialog.show();

            }
        });

        return convertView;
    }

    public void addAll(Collection<? extends Schedule> collection){
        scheduleInfoList.addAll(collection);
        notifyDataSetChanged();
    }
    public static  class Viewholder{
        private final TextView procedureType,proBeginTime,proEndTime,numDay,isFinish;
        private final ImageView delBtn;
        public  Viewholder(View item){
            procedureType = (TextView) item.findViewById(R.id.procedure_type);
            proBeginTime = (TextView) item.findViewById(R.id.pro_begin_time);
            proEndTime = (TextView) item.findViewById(R.id.pro_end_time);
            numDay = (TextView) item.findViewById(R.id.num_day);
            isFinish = (TextView) item.findViewById(R.id.is_finish);
            delBtn = (ImageView)item.findViewById(R.id.del_btn);

        }
    }

    public void delData(int position){
        Schedule schedule = scheduleInfoList.get(position);
        BaseRetrofit.GetBaseRetrofit().delApplicationList(schedule.getId()).enqueue(new Callback<ReturnVo<String>>() {
            @Override
            public void onResponse(Call<ReturnVo<String>> call, Response<ReturnVo<String>> response) {
                if (response.isSuccessful()&&response.body().getCode().equals(1)){
                    ShowToast.CenterPicToast("移除成功",context,R.drawable.ic_check_circle_black_24dp);
                }else {
                    ShowToast.CenterPicToast("移除失败", context, R.drawable.ic_clear_black_24dp);
                }
            }

            @Override
            public void onFailure(Call<ReturnVo<String>> call, Throwable t) {

            }
        });

    }
}
